﻿using System;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace Cef.Utility
{
    public class Computer
    {
        /// <summary>
        /// 查找cpu的id
        /// </summary>
        /// <returns></returns>
        public string CPU_Id
        {
            get
            {
                try
                {
                    string str = string.Empty;
                    ManagementClass mcCPU = new ManagementClass("win32_Processor");
                    ManagementObjectCollection mocCPU = mcCPU.GetInstances();
                    foreach (ManagementObject m in mocCPU)
                    {
                        str = m["Processorid"].ToString().Trim().Substring(0, 8);
                        break;
                    }

                    return str;
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
        }

        public string Disk_Id
        {
            get
            {
                try
                {
                    string hdId = string.Empty;
                    ManagementClass hardDisk = new ManagementClass("win32_DiskDrive");
                    ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
                    foreach (ManagementObject m in hardDiskC)
                    {
                        hdId = m.Properties["Model"].Value.ToString(); //WDC WD800BB-56JKC0
                        break;
                    }

                    return hdId;
                }
                catch (Exception)
                {

                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// 网卡
        /// </summary>
        public string NetworkCard
        {
            get
            {
                try
                {
                    string MoAddress = string.Empty;
                    ManagementClass networkAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    ManagementObjectCollection adapterC = networkAdapter.GetInstances();
                    foreach (ManagementObject m in adapterC)
                    {
                        if ((bool) m["IPEnabled"] == true)
                        {
                            MoAddress = m["MacAddress"].ToString().Trim();
                            m.Dispose();
                        }
                    }

                    return MoAddress;
                }
                catch
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// 获取序列号，制造商，型号
        /// </summary>
        public Tuple<string, string, string> SerialNumber_Manufacturer_Product
        {
            get
            {
                try
                {
                    Tuple<string, string, string> tuple = null;
                    new Tuple<string, string, string>(string.Empty,
                        string.Empty, string.Empty);
                    ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_baseboard");


                    foreach (ManagementObject m in mos.Get())
                    {
                        tuple = new Tuple<string, string, string>(m["SerialNumber"].ToString(),
                            m["Manufacturer"].ToString(),
                            m["Product"].ToString());
                    }

                    return tuple;
                }
                catch (Exception)
                {

                    return null;
                }
            }
        }

        /// <summary>
        /// 计算机名称
        /// </summary>
        public string HostName
        {
            get { return System.Net.Dns.GetHostName(); }
        }
    }
}
